# coding:utf-8
import re
import sys
import time, datetime

reload(sys)
sys.setdefaultencoding("utf-8")

import os



class Res():
    def __init__(self, msg, date, time):
        self.msg = msg
        self.date = date
        self.time = time


class ResOuter():
    def __init__(self):
        self.resList = []
        self.lastData = ""
        self.lastTime = 0.0
        self.index = 1

    def collect(self, res):
        if res is None: return
        self.resList.append(res)

    def save(self):
        path = "/Users/yutianran/Space-Study/Python学习/gitParser/res"
        if os.path.exists(path) == False:
            os.mkdir(path)
        os.chdir(path)

        fos = open('周报.txt', 'wb')
        for res in reversed(self.resList):
            if res.date == self.lastData:
                h = (res.time - self.lastTime) / 3600.0
                x=h-int(h)
                if x <0.5:
                    t=int(h)+0.5
                else:
                    t = int(h) + 1
                if t>2:
                    t=2
                self.index += 1
                fos.write(str(self.index) + "." + res.msg + "(" + str(t) + "h)" + "\n")
                self.lastData = res.date
                self.lastTime = res.time
            else:
                t = 1
                self.index = 1
                fos.write("\n" + res.date + "\n" + "\n")
                fos.write(str(self.index) + "." + res.msg + "(" + str(t) + "h)" + "\n")
                self.lastData = res.date
                self.lastTime = res.time
        fos.close()


def parseLine(line):
    m = line.split(" ")[0]
    t = float(line.split(" ")[-2])
    d = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(t))
    r = d.split(" ")[0]
    return Res(m, r, t)

if __name__ == "__main__":
    path = "/Users/yutianran/Space-Work/车城/历史版本/Che-Android"
    os.chdir(path)
    cmd = 'git log --date=raw --pretty=format:"%s %an %ad" --no-merges --since=8.days  --author=Yutianran'
    stream = os.popen(cmd)
    lines = stream.readlines()
    print "lines=", lines
    resOuter = ResOuter()
    for line in lines:
        res = parseLine(line)
        resOuter.collect(res)
    resOuter.save()

